export default function () {

    /**
     * Promise 忽略错误
     * */
    Promise.prototype.ignoreError = function () {
        return this.catch(() => {
            console.log("[Promise] IgnoreError");

        });
    }

    /**
     * Promise 忽略错误并终止执行
     * */
    Promise.prototype.ignoreErrorAndAbort = function () {
        return this.catch(() => {
            console.log("[Promise] IgnoreErrorAndAbort");
            return new Promise(() => {
            });
        })

    }

    /**
     * Date 日期格式化 yyyy-MM-dd
     */
    Date.prototype.toDateString = function () {

        // 验证入参是否有效
        if (this === null || this === undefined)
            return "";

        const month = (this.getMonth() + 1).toString().padStart(2, '0');
        const strDate = this.getDate().toString().padStart(2, '0');
        let result = `${this.getFullYear()}-${month}-${strDate}`;
        return result;
    }

    /**
     * Date 时间格式化 yyyy-MM-dd HH:mm:ss
     */
    Date.prototype.toDateTimeString = function () {

        // 验证入参是否有效
        if (this === null || this === undefined)
            return "";

        const month = (this.getMonth() + 1).toString().padStart(2, '0');
        const strDate = this.getDate().toString().padStart(2, '0');
        const starHours = this.getHours().toString().padStart(2, '0');
        const starMinutes = this.getMinutes().toString().padStart(2, '0');
        const starSeconds = this.getSeconds().toString().padStart(2, '0');
        let result = `${this.getFullYear()}-${month}-${strDate} ${starHours}:${starMinutes}:${starSeconds}`;
        return result;
    }

    /**
     * String 日期格式化 yyyy-MM-dd
     */
    String.prototype.toDateString = function () {
        let newStr = this.replace(/-/g, "/");
        let date = new Date(newStr);
        return date.toDateString();
    }

    /**
     * String 时间格式化 yyyy-MM-dd HH:mm:ss
     */
    String.prototype.toDateTimeString = function () {
        let newStr = this.replace(/-/g, "/");
        let date = new Date(newStr);
        return date.toDateTimeString();
    }

    /**
     * 转换省略号字符串
     */
    String.prototype.ellipsis = function (length) {
        let result = this.length > length ? `${this.substring(0, length)}...` : this;
        return result;
    }

}